<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456",
      },

      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },

  methods: {
    ...mapActions({
      logins: "login/asyncLogin",
    }),
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
            const res = await this.logins(this.ruleForm);
            console.log(res);

            if (res.data.meta.status === 200) {
              this.$notify({
                title: "登录",
                message: "成功",
                type: "success",
              });
               this.$router.push("/home");
            }
        } else {
          console.log('submit error')
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style>
.login_container {
  width: 100%;
  height: 100%;
  background: #2b4b6b;
}
.login_box {
  width: 400px;
  height: 300px;
  position: absolute;
  transform: translate(-50%, -50%);
  background: #fff;
  top: 50%;
  left: 50%;
  border-radius: 5%;
}
.avatar_box {
  width: 130px;
  height: 130px;
  border: 1px solid #ccc;
  border-radius: 50%;
  transform: translateY(-50%);
  margin: 0 auto;
  background: #fff;
  overflow: hidden;
  box-shadow: 0 0 10px #fff;
}
.avatar_box img {
  width: 100%;
  height: 100%;
}
.login_form {
  position: absolute;
  bottom: 0;
  padding: 0 20px;
  width: 100%;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>